﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain.Query;
using Zero.DataLayer.Donnees;
using Zero.POCO.Compagne;
using Zero.POCO.Enum;
using Zero.POCO.Interfaces;

namespace Zero.DataLayer.Repository
{
    public class PlanPrRepository : IRepositoryPlan<PlanPresse, Guid>
    {
        public PlanPresse FindBy(Guid id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PlanPresse> FindAll()
        {
            var listEntities = new List<PlanPresse>();

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                    {
                        ConnectionString = ((string)config.GetValue("connect", typeof(string)))
                    }){
                var xpoEntities = new XPCollection<XpoPlanPresse>(uow);
                listEntities.AddRange(MAP.MapInverse.MapPlanPresse(xpoEntities));
            }return listEntities;
        }

        public IEnumerable<PlanPresse> FindBy(Query query)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PlanPresse> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }

        public void SaveAll(IEnumerable<PlanPresse> entities)
        {
            throw new NotImplementedException();
        }

        public List<PlanPresse> FindAll(List<Query> entities)
        {
            throw new NotImplementedException();
        }

        public void Save(PlanPresse entity)
        {

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                    {
                        ConnectionString =  ((string)config.GetValue("connect", typeof(string)))
                    })
            {
                var comp = MAP.Map.MapPlanPresse(entity, uow);
                uow.CommitChanges();
            }
        }

        public void Add(PlanPresse entity)
        {
            throw new NotImplementedException();
        }

        public void Remove(PlanPresse entity)
        {
            throw new NotImplementedException();
        }

        public double GetTarif(Query query)
        {
            double prix = 0;
            var criterias = query.Criteria;
            var criteriaPlanTv = criterias.First(e => e.PropertyName == "PlanPr");
            var plantv = (PlanPresse)criteriaPlanTv.Value;
            var listeMap = plantv.ListeMapSupportTarifs;
            if (criterias != null)
            {
                var criteriaInsertionTv = criterias.First(e => e.PropertyName == "InsertionPr");
                var insertiontvs = (List<InsertionPresse>)criteriaInsertionTv.Value;

                if (listeMap == null) return 0;
                foreach (var insertionTv in insertiontvs)
                {
                    MapSupportTarif maptarif = null;
                    IEnumerable<MapSupportTarif> maptarifs = listeMap.Where(e => e.SupportId == insertionTv.SupportPresse.id);
                    if (maptarifs.Any())
                        maptarif = maptarifs.FirstOrDefault();
                    else continue;
                    if (maptarif != null)
                    {
                        List<LignesTarifTv> tarifs = null;

                        tarifs = maptarif.TarifTv.LignesTarifTvs.Where(lt => (FormatPresse)lt.duree == insertionTv.FormatPresse).ToList();
                        if (!tarifs.Any())
                            tarifs = maptarif.TarifTv.LignesTarifTvs.Where(lt => lt.duree == 0).ToList();
                        if (tarifs.Any())
                        {
                            var LaLigne = tarifs.FirstOrDefault();
                            if (LaLigne != null) prix = prix + LaLigne.Prix;
                        }
                    }
                }
            }
            return prix;
        }
    }
}